#! /usr/bin/env python
#coding=utf-8

import uocbase
import time

class Uoc(uocbase.UocBase):
    """Uoc_s 查询所有记录，主要调试用"""
    uoclevel = 'normal'
    
    def direct_parse(self):
        "直接型命令分析"
        # 后面的参数表示返回的值
        try:
            if self.uocparams and self.uocparams[0] == 'all':
                # 查询所有记录
                self.aoclist.append(['s', self.uocparams])
        except:
            pass
        
    def interact_parse(self):
        "交互性命令分析"
        # 当前可实现查询：按年/月/周 查询
        stype = None
        while not stype:
            st = self.get_input('请输入查看类型（1:按周;2:按月;3:按年）默认为按周')
            if not st:
                return None
            if st == 'default':
                stype = 1
                break
            else:
                stype = uocbase.field_validate(st, 'int')
                if stype == 1 or stype == 2 or stype == 2:
                    break
                else:
                    print '格式输入错误'
        
        rdatetime = None
        while not rdatetime:
            st = self.get_input('请输入时间（默认为当前时间）')
            if not st:
                return None
            if st == 'default':
                rdatetime = int(time.time())
                break
            else:
                rdatetime = uocbase.field_validate(st, 'datetime')
                if rdatetime:
                    rdatetime = int(rdatetime)
                    break
                else:
                    print '格式输入错误'
        if (not rdatetime) or (not stype):
            return
        
        timerange = None
        if stype == 1:
            timerange = self.caltime_by_week(rdatetime)
        elif stype == 2:
            timerange = self.caltime_by_month(rdatetime)
        elif stype == 3:
            timerange = self.caltime_by_year(rdatetime)
        
        if not timerange:
            print '查询时间段 从 %s 到 %s ' % (time.ctime(timerange[0]), time.ctime(timerange[1]))
        
        self.aoclist.append(['s', timerange])
